package pdf;

import java.awt.Rectangle;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import org.apache.pdfbox.text.TextPosition;

public class ExtractTextByArea extends PDFTextStripper {
	private int xBegin = 0;
	private int yBegin = 0;
	private int xEnd = 0;
	private int yEnd = 0;

	public int getxBegin() {
		return xBegin;
	}

	public void setxBegin(int xBegin) {
		this.xBegin = xBegin;
	}

	public int getyBegin() {
		return yBegin;
	}

	public void setyBegin(int yBegin) {
		this.yBegin = yBegin;
	}

	public int getxEnd() {
		return xEnd;
	}

	public void setxEnd(int xEnd) {
		this.xEnd = xEnd;
	}

	public int getyEnd() {
		return yEnd;
	}

	public void setyEnd(int yEnd) {
		this.yEnd = yEnd;
	}


	/**
	 * This will print the documents data.
	 *
	 * @param args
	 *            The command line arguments.
	 *
	 * @throws IOException
	 *             If there is an error parsing the document.
	 */

	public ExtractTextByArea() throws IOException {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Override the default functionality of PDFTextStripper.
	 * 覆盖重写PDFTextStripper的默认功能。
	 */
	protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
		int offset = -20;
		if (StringUtils.contains(string, "参考文献:")) {
			this.xBegin = (int) textPositions.get(0).getXDirAdj()+ offset;
			this.yBegin = (int) textPositions.get(0).getYDirAdj();
		}
		/*
		else if (StringUtils.contains(string, "责任编辑")) {
			this.xEnd = this.getxBegin();
			this.yEnd = (int) textPositions.get(0).getYDirAdj()-10;
		}
		*/
		else
		{
			this.xEnd=0;
			this.yEnd=500;
		}
	}
	/**
	 * 截取区域内容
	 * @param xBegin
	 * @param yBegin
	 * @param xEnd
	 * @param yEnd
	 * @param page
	 * @return
	 * @throws IOException
	 */
	private String getContent(int xBegin,int yBegin,int xEnd,int yEnd,PDPage page) throws IOException {
		PDFTextStripperByArea stripperArea = new PDFTextStripperByArea();
		stripperArea.setSortByPosition(true);
		// Rectangle rect = new Rectangle(stripper.getxBegin(), stripper.getyBegin(),
		// stripper.getxEnd()-stripper.getxBegin(),
		// stripper.getyEnd()-stripper.getyBegin());
		Rectangle rect = new Rectangle(xBegin, yBegin, xEnd-xBegin,yEnd - yBegin);
		stripperArea.addRegion("rect", rect);
		stripperArea.extractRegions(page);
		return stripperArea.getTextForRegion("rect");
	}
	
	public String getContent(int pageNum,PDDocument document) throws IOException {
		setSortByPosition(true);
		setStartPage(pageNum+1);
		setEndPage(pageNum+1);
		
		Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
		writeText(document, dummy);

		PDFTextStripperByArea stripperArea = new PDFTextStripperByArea();
		stripperArea.setSortByPosition(true);
		PDPage firstPage = (PDPage) document.getPage(pageNum);
		Rectangle rect = new Rectangle(getxBegin(), getyBegin(), (int)firstPage.getMediaBox().getWidth()-getxBegin(),
				getyEnd() - getyBegin());
		stripperArea.addRegion("class1", rect);
		// List allPages = document.getDocumentCatalog().getAllPages();
		int num = document.getNumberOfPages();

		stripperArea.extractRegions(firstPage);
		return stripperArea.getTextForRegion("class1");
		
	}
	
	public static void main(String[] args) throws Exception {
		String file = "resources/demo.pdf";
		try (PDDocument document = PDDocument.load(new File(file))) {
			ExtractTextByArea stripper = new ExtractTextByArea();
			stripper.setSortByPosition(true);//设置排序
			int num = document.getNumberOfPages();
			stripper.setStartPage(num);//设置起始页
			stripper.setEndPage(num);//设置结束页
			
			Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
			stripper.writeText(document, dummy); //调用writeString 定位坐标

			PDFTextStripperByArea stripperArea = new PDFTextStripperByArea();
			stripperArea.setSortByPosition(true);
			//最后一页
			PDPage firstPage = (PDPage) document.getPage(num-1);
			Rectangle rect = new Rectangle(stripper.getxBegin(), stripper.getyBegin(), 
					(int)firstPage.getMediaBox().getWidth()-stripper.getxBegin(),
					stripper.getyEnd() - stripper.getyBegin());
			stripperArea.addRegion("class1", rect);
			// List allPages = document.getDocumentCatalog().getAllPages();

			stripperArea.extractRegions(firstPage);
			System.out.println("区域:" + rect);
			System.out.println("解析出来的：");
			System.out.println(stripperArea.getTextForRegion("class1"));
			
			document.close();
		}

	}
}

